5月 8 2015 Online Judge►LeetCode [LeetCode] 125 - Valid Palindrome 題意判斷字串是否回文,只考慮字母和數字。 解法兩個指針一個從頭一個從尾,遇到非字母數字的跳過即可。 程式12345678910111213141516171819202122232425class Solution {public: bool isPalindrome(string s) { int index1 = 0 , index2 = s.length() - 1 ; while ( index1 < index2 ){ if (!(s[index1]>='0'&&s[index1]<='9'||s[index1]>='a'&&s[index1]<='z'||s[index1]>='A'&&s[index1]<='Z') ){ index1 ++ ; continue ; } if ( s[index1] >= 'A' && s[index1] <= 'Z' ) s[index1] += 32 ; if (!(s[index2]>='0'&&s[index2]<='9'||s[index2]>='a'&&s[index2]<='z'||s[index2]>='A'&&s[index2]<='Z') ){ index2 -- ; continue ; } if ( s[index2] >= 'A' && s[index2] <= 'Z' ) s[index2] += 32 ; if ( s[index1] != s[index2] ) return false ; index1 ++ ; index2 -- ; } return true ; }}; Newer [LeetCode] 62 - Unique Paths Older [Web] Google Map API簡易使用